/* eslint-disable */
module.exports = function (redisClient, module) {
    module.sortedSetUnionCard = function (keys, callback) {
        const tempSetName = `temp_${Date.now()}`;
        const multi = redisClient.multi();
        multi.zunionstore([tempSetName, keys.length].concat(keys));
        multi.zcard(tempSetName);
        multi.del(tempSetName);
        multi.exec((err, results) => {
            if (err) {
                return callback(err);
            }
            callback(null, Array.isArray(results) && results.length ? results[1] : 0);
        });
    };

    module.getSortedSetUnion = function (params, callback) {
        params.method = 'zrange';
        module.sortedSetUnion(params, callback);
    };

    module.getSortedSetRevUnion = function (params, callback) {
        params.method = 'zrevrange';
        module.sortedSetUnion(params, callback);
    };

    module.sortedSetUnion = function (params, callback) {
        const tempSetName = `temp_${Date.now()}`;

        const rangeParams = [tempSetName, params.start, params.stop];
        if (params.withScores) {
            rangeParams.push('WITHSCORES');
        }

        const multi = redisClient.multi();
        multi.zunionstore([tempSetName, params.sets.length].concat(params.sets));
        multi[params.method](rangeParams);
        multi.del(tempSetName);
        multi.exec((err, results) => {
            if (err) {
                return callback(err);
            }
            if (!params.withScores) {
                return callback(null, results ? results[1] : null);
            }
            results = results[1] || [];
            const objects = [];
            for (let i = 0; i < results.length; i += 2) {
                objects.push({ value: results[i], score: parseFloat(results[i + 1]) });
            }
            callback(null, objects);
        });
    };
};
/* eslint-enable */
